﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Managers.*;
    import Events.*;
    import flash.utils.*;
    import Display.*;
    import Classes.virals.*;
    import Modules.realtime.*;
    import Engine.*;

    public class NeighborVisitManager {

        public static const VISIT_DEFAULT:int = -1;
        public static const VISIT_TOURBUS:int = 0;
        public static const VISIT_REVIVEDCROPS:int = 1;
        public static const VISIT_CLEANEDSHIPS:int = 2;
        public static const VISIT_FINISHEDBUILDING:int = 3;
        public static const VISIT_FLAGCOUNT:int = 4;

        private static var m_VisitFlags:Array = new Array();
        private static var m_VisitType:int = -1;
        private static var m_TriggersViralBox:Boolean = false;

        public function NeighborVisitManager(){
            var _local1:int;
            while (_local1 < VISIT_FLAGCOUNT) {
                m_VisitFlags.push(false);
                _local1++;
            };
        }
        public static function getVisitFlag(_arg1:int):Boolean{
            return (m_VisitFlags[_arg1]);
        }
        public static function setVisitFlag(_arg1:int, _arg2:Boolean=true):void{
            m_VisitFlags[_arg1] = _arg2;
        }
        public static function resetVisitFlags():void{
            var _local1:int;
            while (_local1 < VISIT_FLAGCOUNT) {
                setVisitFlag(_local1, false);
                _local1++;
            };
            m_VisitType = VISIT_DEFAULT;
            m_TriggersViralBox = false;
        }
        private static function countVisitFlagsSetToTrue():int{
            var _local1:int;
            var _local2:int;
            while (_local2 < VISIT_FLAGCOUNT) {
                if (getVisitFlag(_local2)){
                    _local1++;
                };
                _local2++;
            };
            return (_local1);
        }
        private static function areAllFlagsFalse():Boolean{
            return ((countVisitFlagsSetToTrue() == 0));
        }
        private static function isOnlyOneFlagTrue():Boolean{
            return ((countVisitFlagsSetToTrue() == 1));
        }
        private static function moreThanOneFlagTrue():Boolean{
            return (((!(areAllFlagsFalse())) && (!(isOnlyOneFlagTrue()))));
        }
        private static function getSingleTrueFlag():int{
            var _local1:int = VISIT_DEFAULT;
            var _local2:int;
            while (_local2 < VISIT_FLAGCOUNT) {
                if (getVisitFlag(_local2)){
                    _local1 = _local2;
                    break;
                };
                _local2++;
            };
            return (_local1);
        }
        private static function pickRandomTrueFlag():int{
            var _local1:Array = new Array();
            var _local2:int;
            while (_local2 < VISIT_FLAGCOUNT) {
                if (getVisitFlag(_local2)){
                    _local1.push(_local2);
                };
                _local2++;
            };
            var _local3:int = Math.round(Utilities.randBetween(0, (_local1.length - 1)));
            return (_local1[_local3]);
        }
        private static function sendRTVisit():void{
            var _local2:String = arguments[0];
            if (((_local2) && (!((_local2 == "-1"))))){
                RealtimeManager.callMethodOnOnlineUser(_local2, new RealtimeMethod("showNeighborVisit"));
            };
        }
        public static function sendRTNeighborVisitMessage():void{
            var _local1:String = Global.getVisiting();
            if (Global.player.didVisitAction){
                setTimeout(sendRTVisit, TransactionManager.amfMaxWait, _local1);
            };
        }
        public static function triggerNeighborVisitFeeds():void{
            var _local1:int = VISIT_DEFAULT;
            var _local2 = "";
            var _local3:String = ViralType.NEIGHBOR_VISIT_DEFAULT;
            if (m_TriggersViralBox){
                return;
            };
            m_TriggersViralBox = true;
            var _local4:String = Global.getVisiting();
            if (_local4 == Player.FAKE_USER_ID_STRING){
                return;
            };
            if (isOnlyOneFlagTrue()){
                _local1 = getSingleTrueFlag();
            } else {
                if (moreThanOneFlagTrue()){
                    _local1 = pickRandomTrueFlag();
                };
            };
            m_VisitType = _local1;
            var _local5 = "assets/dialogs/feeds/feed_givefriendcoinbonus.png";
            switch (m_VisitType){
                case VISIT_TOURBUS:
                    _local2 = "neighborVisit_prompt_bus";
                    _local5 = "assets/dialogs/feeds/feed_tourbus.png";
                    _local3 = ViralType.NEIGHBOR_VISIT_BUS;
                    break;
                case VISIT_REVIVEDCROPS:
                    _local2 = "neighborVisit_prompt_crop";
                    _local5 = "assets/dialogs/feeds/feed_revivecrops.png";
                    _local3 = ViralType.NEIGHBOR_VISIT_CROPS;
                    break;
                case VISIT_CLEANEDSHIPS:
                    _local2 = "neighborVisit_prompt_ship";
                    _local5 = "assets/dialogs/feeds/feed_reviveships.png";
                    _local3 = ViralType.NEIGHBOR_VISIT_SHIPS;
                    break;
                case VISIT_FINISHEDBUILDING:
                    _local2 = "neighborVisit_prompt_build";
                    _local5 = "assets/dialogs/feeds/feed_buildingready.png";
                    _local3 = ViralType.NEIGHBOR_VISIT_BUILD;
                    break;
                default:
                    _local2 = "neighborVisit_prompt_default";
            };
            UI.displayViralShareMessage(_local3, ZLoc.t("Dialogs", _local2), GenericPopup.TYPE_SHARECANCEL, onNeighborVisitClosed, "neighborVisit", true, _local5, "neighborVisit");
        }
        private static function onNeighborVisitClosed(_arg1:GenericPopupEvent):void{
            _arg1.target.removeEventListener(GenericPopupEvent.SELECTED, onNeighborVisitClosed);
            if (_arg1.button != GenericPopup.YES){
                return;
            };
            var _local2:String = Global.getVisiting();
            var _local3:String = Global.player.getFriendCityName(_local2);
            switch (m_VisitType){
                case VISIT_TOURBUS:
                    Global.world.viralMgr.sendNeighborVisitFeed_Bus(Global.player, Global.player.cityName, _local2, _local3);
                    break;
                case VISIT_REVIVEDCROPS:
                    Global.world.viralMgr.sendNeighborVisitFeed_Crop(Global.player, _local2);
                    break;
                case VISIT_CLEANEDSHIPS:
                    Global.world.viralMgr.sendNeighborVisitFeed_Ship(Global.player, _local2);
                    break;
                case VISIT_FINISHEDBUILDING:
                    Global.world.viralMgr.sendNeighborVisitFeed_Build(Global.player, _local2);
                    break;
                default:
                    Global.world.viralMgr.sendNeighborVisitFeed_Default(Global.player, _local2, _local3);
            };
        }

    }
}//package Classes 
